package com.info33.platform.system.controller;

import java.text.ParseException;
import java.util.List;

import javax.validation.Valid;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.system.vo.SysAnnouncementVO;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.info33.platform.system.dto.SysAnnouncementDTO;
import com.info33.platform.system.entity.SysAnnouncement;
import com.info33.platform.system.service.SysAnnouncementService;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.entity.SysDictionary;
import com.info33.platform.system.service.SysDictionaryService;
import com.info33.platform.system.service.SysFileService;
import com.info33.platform.system.util.enums.UserTypeEnum;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/SysAnnouncement")
@Api(tags = "系统 - 公告表")
public class SysAnnouncementController extends BaseController<SysAnnouncementService,SysAnnouncement> {

	private final SysAnnouncementService sysAnnouncementService;
	
	public SysAnnouncementController(SysAnnouncementService baseService) {
		super(baseService);
		this.sysAnnouncementService = baseService;
	
	}
	
	@Autowired
	SysDictionaryService sysDictionaryService;
	
	@Autowired
	SysFileService sysFileService;

	/********************************************************************************
	 * @Title: get
	 * @Description: 通过id获取公告表详情
	 ********************************************************************************/
	@GetMapping(value = "/{id}")
	@ApiOperation(value = "获取公告表详情", notes = "通过ID获取公告表详情")
	public Result<SysAnnouncement> get(@PathVariable(value = "id") Long id) throws Exception {
		Long tenantId = getTenantId();
		return Result.ok( sysAnnouncementService.get(id,tenantId));
	}


	/********************************************************************************
	 * @Title: getlist
	 * @Description: 通过条件获取公告表全部列表
	 ********************************************************************************/
	@PostMapping(value = "/list")
	@ApiOperation(value = "获取公告表详情", notes = "通过条件获取公告表详情 pageNum 和 pageSize 都为 0 的时候就是查询全部数据")
	public Result<List<SysAnnouncement>> getlist(@RequestBody SysAnnouncement sysAnnouncement) throws Exception {
		return Result.ok(sysAnnouncementService.list(new QueryWrapper<SysAnnouncement>(sysAnnouncement)));
	}
	
	/********************************************************************************
	 * @Title: getlist
	 * @Description: 通过条件获取公告表分页列表
	 ********************************************************************************/
	@PostMapping(value = "/hisList/page/{pageNum}/{pageSize}")
	@ApiOperation(value = "获取公告历史表详情", notes = "通过条件获取公告历史表详情")
	public Result<IPage<SysAnnouncement>> getHisList(@RequestBody SysAnnouncement sysAnnouncement, @PathVariable int pageNum, @PathVariable int pageSize) throws Exception {
		Long tenantUserId = getTenantUserId();
		Long userId = getCurrentUser().getId();
		Long tenantId = getTenantId();
		UserTypeEnum userType =getCurrentUser().getSysTenantUserVO().getUserType();
		return Result.ok(sysAnnouncementService.getHisListByAuth(userId,tenantUserId,tenantId,userType,sysAnnouncement, pageNum, pageSize));
		
	}
	@PostMapping(value = "/list/page/{pageNum}/{pageSize}")
	@ApiOperation(value = "获取公告表详情", notes = "通过条件获取公告表详情")
	public Result<IPage<SysAnnouncement>> getlist(@RequestBody SysAnnouncement sysAnnouncement, @PathVariable int pageNum, @PathVariable int pageSize) throws Exception, ParseException {
		Long tenantUserId = getTenantUserId();
		Long userId = getCurrentUser().getId();
		Long tenantId = getTenantId();
		UserTypeEnum userType =getCurrentUser().getSysTenantUserVO().getUserType();
		return Result.ok(sysAnnouncementService.getListByAuth(userId,tenantUserId,tenantId,userType,sysAnnouncement, pageNum, pageSize));
		
	}
	/********************************************************************************
	 * @throws Exception 
	 * @Title: add
	 * @Description: 新增单个公告表详情
	 ********************************************************************************/
	@PostMapping("/add")
	@ApiOperation(value = "新增单个公告表详情", notes = "新增单个公告表详情")
	public Result<SysAnnouncement> add(@Valid @RequestBody SysAnnouncementDTO sysAnnouncementDTO, BindingResult bindingResult)  throws Exception {
		SysAnnouncement sysAnnouncement = new SysAnnouncement();
		BeanUtils.copyProperties(sysAnnouncementDTO, sysAnnouncement);
		Long userId = getTenantUserId();
		Long tenantId = getTenantId();
		sysAnnouncement.setCurrentId(userId.toString());
		sysAnnouncement.setTenantId(tenantId);
		return sysAnnouncementService.add(sysAnnouncement);
	
	}
	

	/********************************************************************************
	 * @Title: addBatch
	 * @Description: 新增多个公告表详情
	 ********************************************************************************/
	@PostMapping("/addBatch")
	@ApiOperation(value = "新增多个公告表详情", notes = "新增多个公告表详情")
	public Result<List<SysAnnouncement>> addBatch(@RequestBody List<SysAnnouncement> sysAnnouncementList)  throws Exception {
		//批量的校验无效，需要自己写校验
		return sysAnnouncementService.saveBatch(sysAnnouncementList)?Result.ok(sysAnnouncementList):Result.error();
	}

	/********************************************************************************
	 * @throws Exception 
	 * @Title: update
	 * @Description: 更新单个公告表详情
	 ********************************************************************************/
	@PostMapping("/edit")
	@ApiOperation(value = "更新单个公告表详情", notes = "更新单个公告表详情")
	//@RequiresPermissions("system:announcementManagement:announcement:edit")
	public Result<?> edit(@Valid @RequestBody SysAnnouncement sysAnnouncement, BindingResult bindingResult)  throws Exception {
		String userId = getTenantUserId().toString();
		UserTypeEnum userType =getCurrentUser().getSysTenantUserVO().getUserType();
		sysAnnouncement.setCurrentId(userId);
		Long tenantId = getTenantId();
		sysAnnouncement.setTenantId(tenantId);
		SysAnnouncement sysAnnouncements=sysAnnouncementService.getOne(new QueryWrapper<SysAnnouncement>().eq("id", sysAnnouncement.getId()));
		if("ADMIN".equals(userType.ADMIN.name())) {
			return sysAnnouncementService.edit(sysAnnouncement);
		}else if(sysAnnouncements.getCurrentId().equals(userId)) {
			return sysAnnouncementService.edit(sysAnnouncement);
		}else {
		return Result.error("您没有权限修改该条记录！");
		}
	}
	
	@PostMapping("/change")
	@ApiOperation(value = "点击编辑按钮时判断是否跳到编辑详情页", notes = "点击编辑按钮时判断是否跳到编辑详情页")
	public Result<?> change(@Valid @RequestBody SysAnnouncement sysAnnouncement, BindingResult bindingResult)  throws Exception {
		String userId = getTenantUserId().toString();
		UserTypeEnum userType =getCurrentUser().getSysTenantUserVO().getUserType();
		SysAnnouncement sysAnnouncements=sysAnnouncementService.getOne(new QueryWrapper<SysAnnouncement>().eq("id", sysAnnouncement.getId()));
		if("ADMIN".equals(userType.ADMIN.name())) {
			return Result.ok("您确定修改该条公告？");
		}else if(sysAnnouncements.getCurrentId().equals(userId)) {
			return Result.ok("您确定修改该条公告？");
		}else {
		return Result.error("您没有权限修改该条公告！");
		}
	}
	

	/********************************************************************************
	 * @Title: editBatch
	 * @Description: 更新多个公告表详情
	 ********************************************************************************/
	@PostMapping("/editBatch")
	@ApiOperation(value = "更新多个公告表详情", notes = "更新多个公告表详情")
	public Result<List<SysAnnouncement>> editBatch(@RequestBody List<SysAnnouncement> sysAnnouncementList)  throws Exception {
		//批量的校验无效，需要自己写校验
		return sysAnnouncementService.updateBatchById(sysAnnouncementList)?Result.ok(sysAnnouncementList):Result.error();
	}

	/********************************************************************************
	 * @throws Exception 
	 * @Title: remove
	 * @Description: 删除单个公告表详情
	 ********************************************************************************/
	@PostMapping("/remove/{id}")
	@ApiOperation(value = "删除单个公告表详情", notes = "删除单个公告表详情")
	//@RequiresPermissions("system:announcementManagement:announcement:delete")
	public Result<?> remove(@PathVariable(value = "id") Long id)  throws Exception {
		String userId = getTenantUserId().toString();
		UserTypeEnum userType =getCurrentUser().getSysTenantUserVO().getUserType();
		SysAnnouncement sysAnnouncement=sysAnnouncementService.getOne(new QueryWrapper<SysAnnouncement>().eq("id", id));
		if("ADMIN".equals(userType.ADMIN.name())) {
			return sysAnnouncementService.remove(id)>0?Result.ok():Result.error();
		}else if(sysAnnouncement.getCurrentId().equals(userId)) {
			return sysAnnouncementService.remove(id)>0?Result.ok():Result.error();
		}else {
		return Result.error("您没有权限删除该条公告！");
		}
	}
	
	@PostMapping(value = "/dictionary/list/{parentId}")
	@ApiOperation(value = "公告类型字典表详情", notes = "公告类型字典表详情")
	public Result<List<SysDictionary>> getAssetlist(@PathVariable(value = "parentId") Long parentId) throws Exception {
		SysDictionary querySysDictionary = new SysDictionary();
		querySysDictionary.setPid(parentId);
		querySysDictionary.setTenantId(getTenantId());
		return Result.ok( sysDictionaryService.list(new QueryWrapper<SysDictionary>(querySysDictionary)));
	}
	@ApiOperation(value = "获取公告列表的附件", notes = "获取公告列表的附件")
	@GetMapping("/getFiles")
	public Result<?> getFiles(){
		Long tenantId = getTenantId();
		return sysAnnouncementService.getFiles(tenantId);
	}	

}
